package xanatorium.Zeiterfassung.data.db;

import xanatorium.Zeiterfassung.data.EntryDAO;
import android.content.ContentProvider;
import android.content.ContentUris;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class EntryProvider extends ContentProvider {

	// The incoming URI matches the Notes URI pattern
	private static final int ENTRIES = 1;

	// The incoming URI matches the Note ID URI pattern
	private static final int ENTRIES_ID = 2;

	// The incoming URI matches the Live Folder URI pattern
	private static final int LIVE_FOLDER_ENTRIES = 3;
	private static UriMatcher sUriMatcher;
	private WorkTimeDictionaryHelper openHelper;

	static {
		sUriMatcher = new UriMatcher(UriMatcher.NO_MATCH);

		// Add a pattern that routes URIs terminated with "notes" to a NOTES
		// operation
		sUriMatcher.addURI(EntryDAO.AUTHORITY, EntryDAO.Entries.TABLE_NAME,
				ENTRIES);

		// Add a pattern that routes URIs terminated with "notes" plus an
		// integer
		// to a note ID operation
		sUriMatcher.addURI(EntryDAO.AUTHORITY, "entries/#", ENTRIES_ID);

		// Add a pattern that routes URIs terminated with live_folders/notes to
		// a
		// live folder operation
		sUriMatcher.addURI(EntryDAO.AUTHORITY,
				EntryDAO.Entries.PATH_LIVE_FOLDER, LIVE_FOLDER_ENTRIES);

	}

	@Override
	public int delete(Uri arg0, String arg1, String[] arg2) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri arg0) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues initialValues) {

		// Validates the incoming URI. Only the full provider URI is allowed for
		// inserts.
		if (sUriMatcher.match(uri) != ENTRIES) {
			throw new IllegalArgumentException("Unknown URI " + uri);
		}

		// A map to hold the new record's values.
		ContentValues values;

		// If the incoming values map is not null, uses it for the new values.
		if (initialValues != null) {
			values = new ContentValues(initialValues);

		} else {
			// Otherwise, create a new value map
			values = new ContentValues();
		}

		// Gets the current system time in milliseconds
		Long now = Long.valueOf(System.currentTimeMillis());

		// If the values map doesn't contain the creation date, sets the value
		// to the current time.
		if (values.containsKey(EntryDAO.Entries.COLUMN_NAME_STARTTIME) == false) {
			values.put(EntryDAO.Entries.COLUMN_NAME_STARTTIME, now);
		}

		// If the values map doesn't contain the modification date, sets the
		// value to the current
		// time.
		if (values.containsKey(EntryDAO.Entries.COLUMN_NAME_ENDTIME) == false) {
			values.put(EntryDAO.Entries.COLUMN_NAME_ENDTIME, now);
		}

		// If the values map doesn't contain a title, sets the value to the
		// default title.
		if (values.containsKey(EntryDAO.Entries.COLUMN_NAME_BREAK) == false) {
			values.put(EntryDAO.Entries.COLUMN_NAME_BREAK, 30);
		}

		// If the values map doesn't contain note text, sets the value to an
		// empty string.
		if (values.containsKey(EntryDAO.Entries.COLUMN_NAME_KEY) == false) {
			values.put(EntryDAO.Entries.COLUMN_NAME_KEY, "");
		}

		// If the values map doesn't contain note text, sets the value to an
		// empty string.
		if (values.containsKey(EntryDAO.Entries.COLUMN_NAME_DESC) == false) {
			values.put(EntryDAO.Entries.COLUMN_NAME_DESC, "");
		}

		// Opens the database object in "write" mode.
		SQLiteDatabase db = openHelper.getWritableDatabase();

		// Performs the insert and returns the ID of the new note.
		long rowId = db.insert(EntryDAO.Entries.TABLE_NAME, // The table to
															// insert into.
				EntryDAO.Entries.COLUMN_NAME_STARTTIME, // A hack, SQLite sets
														// this column value to
														// null
				// if values is empty.
				values // A map of column names, and the values to insert
						// into the columns.
				);
		// If the insert succeeded, the row ID exists.
		if (rowId > 0) {
			// Creates a URI with the note ID pattern and the new row ID
			// appended to it.
			Uri noteUri = ContentUris.withAppendedId(
					EntryDAO.Entries.CONTENT_ID_URI_BASE, rowId);

			// Notifies observers registered against this provider that the data
			// changed.
			getContext().getContentResolver().notifyChange(noteUri, null);
			return noteUri;
		}

		// If the insert didn't succeed, then the rowID is <= 0. Throws an
		// exception.
		throw new SQLException("Failed to insert row into " + uri);
	}

	@Override
	public boolean onCreate() {
		openHelper = new WorkTimeDictionaryHelper(getContext());
		return true;
	}

	@Override
	public Cursor query(Uri arg0, String[] arg1, String arg2, String[] arg3,
			String arg4) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Uri arg0, ContentValues arg1, String arg2, String[] arg3) {
		// TODO Auto-generated method stub
		return 0;
	}

}
